<?php
defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH.'libraries/BaseApiController.php';
class course extends  BaseApiController
{
   public function index(){

   }
   
   public function list_by_page(){
	   $token=$this->verifyToken();
	   if($token){
		   $keys=[
		   		["title","option","课程名称",""],
		   		["type","option","授课方式",""],
		   		["color","option","课程颜色",""]
		   ];
		   
		   $data=$this->getFields($keys);
		   
		   foreach($data as $key=>$value){
		   		 $this->db->like($key,$value); 
		   }
		   $this->db->where("schcode",$token["schcode"]);
		   $res=$this->db->select("count(*) as count")->get("course")->row_array();
		   $numrows=(int)$res["count"];
		   
		   foreach($data as $key=>$value){
		   		 $this->db->like($key,$value); 
		   }
		   
		   $this->db->where("schcode",$token["schcode"]);
		   $rows=$this->db->get("course")->result_array();
		   
		   for($i=0;$i<count($rows);$i++){
			   $rule_ids=explode(",",$rows[$i]["rule_ids"]);
			   if(count($rule_ids)==0){
				    $rows[$i]["rules"]=[];
			   }else{
				   $rules=$this->db->where_in("ruleid",$rule_ids)->get("course_rules")->result_array();
				   $rows[$i]["rules"]=$rules;
			   }
 		   }		   
		   
		   $this->WRITE("code",1);
		   $this->WRITE("numrows",$numrows);
		   $this->WRITE("list",$rows);
		   $this->RESPONSE();	 

	   }
	 
   }
   
   public function create(){
	   $token=$this->verifyToken();
	   if($token){
		   
		   $keys=[
		   		 ["title","required","课程名称",""],
		   		 ["type","required","授课方式",""],
		   		 ["color","required","课程颜色",""],
		   		 ["rules","required","收费策略",[]],
				 ["desc","option","课程简介",""],
				 
		   ];
		   $data=$this->getFields($keys);
		   $rules=$data["rules"];
		   unset($data["rules"]);

		   $data["schcode"]=$token["schcode"];
		   $res=$this->db->insert("course",$data);

		   $courseid=$this->db->insert_id();

			$ruleids=[];
			$finalrules=[];
		   //检测是否新建规格
		   foreach($rules as $rule){
			   if($rule["ruleid"]==-1){
				   //新建 
				   $rule["courseid"]=$courseid;
				   unset($rule["ruleid"]);
				   $this->db->insert("course_rules",$rule);
				   $ruleid=$this->db->insert_id();
				   $rule["ruleid"]=$ruleid;
				   array_push($finalrules,$rule);
				   array_push($ruleids,$ruleid);
			   }else{
				   //更新
				   $rule["courseid"]=$courseid;
				   $this->db->where("ruleid",$rule["ruleid"])->update("course_rules",$rule);
				   array_push($ruleids,$rule["ruleid"]);
				   array_push($finalrules,$rule);
			   }
		   }
		   
		   if(count($ruleids)==0){
		   	$this->db->where("courseid",$courseid)->delete("course_rules");
		   	$data["rule_ids"]="";
		   	$data["rules"]="";
		   }else{
		   	$this->db->where("courseid",$courseid)->where_not_in("ruleid",$ruleids)->delete("course_rules");
		   	$data["rule_ids"]=implode(",",$ruleids);
		   	$data["rules"]=json_encode($finalrules,JSON_UNESCAPED_UNICODE);
		   }
		   
		   //删除无用计费规则
		   $this->db->where("courseid",$courseid)->update("course",$data);
		   
		   if($res){
		   		   $this->WRITE("code",1);
		   		   $this->WRITE("msg","新增课程成功!");
		   		   $this->RESPONSE();	
		   }else{
		   		   $this->WRITE("code",0);
		   		   $this->WRITE("msg","新增课程失败!");
		   		   $this->RESPONSE();	
		   }
		   
		   
	   }
	   

   }
   
   public function update(){
   	   $keys=[
		    ["courseid","required","课程索引",""],
		    ["title","required","课程名称",""],
		    ["type","required","授课方式",""],
		    ["color","required","课程颜色",""],
		    ["rules","required","收费策略",[]],
			["desc","option","课程简介",""]
		];
		
		$data=$this->getFields($keys);
		$rules=$data["rules"];
		unset($data["rules"]);
		$courseid=$data["courseid"];

		
		$data["rule_ids"]="";
		$ruleids=[];
		$finalrules=[];
		//检测是否新建规格
		foreach($rules as $rule){
		   if($rule["ruleid"]==-1){
			   //新建 
			   $rule["courseid"]=$courseid;
			   unset($rule["ruleid"]);
			   $this->db->insert("course_rules",$rule);
			   $ruleid=$this->db->insert_id();
			   $rule["ruleid"]=$ruleid;
			   array_push($ruleids,$ruleid);
			   array_push($finalrules,$rule);
		   }else{
			   //更新
			   $rule["courseid"]=$courseid;
			   $this->db->where("ruleid",$rule["ruleid"])->update("course_rules",$rule);
			   array_push($ruleids,$rule["ruleid"]);
			   array_push($finalrules,$rule);
		   }
		}
		
		//无任何计费策略
		if(count($ruleids)==0){
			$this->db->where("courseid",$courseid)->delete("course_rules");
			$data["rule_ids"]="";
			$data["rules"]="";
		}else{
			$this->db->where("courseid",$courseid)->where_not_in("ruleid",$ruleids)->delete("course_rules");
			$data["rule_ids"]=implode(",",$ruleids);
			$data["rules"]=json_encode($finalrules,JSON_UNESCAPED_UNICODE);
  		}
    	$res=$this->db->where("courseid",$data["courseid"])->update("course",$data);
   	   if($res){
   		   $this->WRITE("code",1);
   		   $this->WRITE("msg","更新课程信息成功!");
   		   $this->RESPONSE();	
   	   }else{
   		   $this->WRITE("code",0);
   		   $this->WRITE("msg","更新课程信息失败!");
   		   $this->RESPONSE();	
   	   }
   
   }
   
   
   public function delete(){
	   
   	   $data=$this->getFields(array(["courseid","required","课程索引",""]));
	   
	   //查询此课程是否已经关联班级
	   $row=$this->db->like("course_ids",$data["courseid"])->get("class")->row_array();
	   if(!$row){
		  $res=$this->db->where("courseid",$data["courseid"])->delete("course",$data);
		  if($res){
		     		   $this->WRITE("code",1);
		     		   $this->WRITE("msg","删除课程信息成功!");
		     		   $this->RESPONSE();	
		  }else{
		     		   $this->WRITE("code",0);
		     		   $this->WRITE("msg","删除课程信息失败!");
		     		   $this->RESPONSE();	
		  } 
	   }else{
		   $this->WRITE("code",0);
		   $this->WRITE("msg","删除失败,此课程已关联班级【".$row["title"]."】");
		   $this->RESPONSE();	
	   }
 
   }
}